const apiBaseUrl = 'https://www.zxperson.com/api/'; //正式网址

export const post = (method, data, callback, showding = true) => {
	uni.request({
		url: apiBaseUrl + method,
		data: data,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/json',
		},
		method: 'POST',
		success: (response) => {
			callback(response.data)
		},
		complete: (res) => {
			// uni.hideLoading();
		},
		fail: (error) => {
			// uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
	});
}
const get = (url, callback) => {
	// uni.showLoading({
	// 	title: '加载中'
	// });
	uni.request({
		url: apiBaseUrl + url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			callback(response.data)
		},
		fail: (error) => {
			// uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			// setTimeout(function() {
			// 	uni.hideLoading();
			// }, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api.html', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				headers: {
					'Accept': 'application/json',
					'Content-Type': 'multipart/form-data',
					'api-type': sysinfo.platform, //自定义请求头信息
				},
				formData: {
					'method': 'images.upload',
					'upfile': tempFilePaths[0]
				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 上传图片
const uploadImage = (num, callback) => {
	uni.chooseImage({
		count: num,
		success: (res) => {
			uni.showLoading({
				title: '上传中...'
			});
			let tempFilePaths = res.tempFilePaths
			for (var i = 0; i < tempFilePaths.length; i++) {
				uni.uploadFile({
					url: apiBaseUrl + 'api.html',
					filePath: tempFilePaths[i],
					fileType: 'image',
					name: 'file',
					headers: {
						'Accept': 'application/json',
						'Content-Type': 'multipart/form-data',
						'api-type': sysinfo.platform, //自定义请求头信息
					},
					formData: {
						'method': 'images.upload',
						'upfile': tempFilePaths[i]
					},
					success: (uploadFileRes) => {
						callback(JSON.parse(uploadFileRes.data));
					},
					fail: (error) => {
						if (error && error.response) {
							showError(error.response);
						}
					},
					complete: () => {
						setTimeout(function() {
							uni.hideLoading();
						}, 250);
					},
				});
			}
		}
	});
}



// 检查登录状态
// export const checkLogin = (callback) => get('checkLogin', callback);
// 工具列表
export const tabs = (callback) => get('resourcelibrary/tabs', callback);

export const getNews = (data,callback) => post('resourcelibrary/getNews',data, callback); 

export const getLibrary = (data,callback) => post('resourcelibrary/getLibrary',data, callback); 

export const search = (data,callback) => post('resourcelibrary/search',data, callback); 

//小程序列表
export const programList = (callback) => get('toolwriting/programList', callback);
// export const answerBook = (callback) => get('tool/answerBook', callback);
// export const sign = (callback) => get('tool/sign', callback); 
// export const hot60s = (callback) => get('tool/hot60s', callback); 
// export const qrCode = (data,callback) => post('tool/qrCode',data, callback); 
// export const horoscope = (data, callback) => post('tool/horoscope', data, callback);
// export const historypic = (callback) => get('tool/historypic', callback);
// export const bmi = (data, callback) => post('tool/bmi', data, callback);
// export const bing = (callback) => get('tool/bing', callback);
// export const analyzeVideo = (data, callback) => post('tool/analyzeVideo', data, callback);

